﻿using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Util;
using DataCenter.Configuration;

namespace DataCenter.Provider
{
    public class IncentiveAdhocProvider : GenericProvider, IIncentiveAdhocProvider
    {
        public IncentiveAdhoc RetrieveIncentiveAdhoc(DateTime selloutDate)
        {
            return repository.FindOne<IncentiveAdhoc>(x => x.CloseFlag == BooleanString.FALSE && x.StartDate <= selloutDate && x.EndDate >= selloutDate && x.PurposeType == SystemConfiguration.PURPOSE_TYPE);
        }


        public List<IncentiveAdhoc> SearchIncentiveAdhoc(out int totalRecord, string textSearch, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<IncentiveAdhoc>();
            if (!string.IsNullOrWhiteSpace(textSearch))
                querry = querry.Where(x => x.SchemeName.ToUpper().Contains(textSearch.ToUpper()));
            querry = querry.OrderBy(x => x.SchemeName);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0) {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public List<IncentiveAdhoc> SearchIncentiveAdhoc(out int totalRecord, string incentiveAdhocName, DateTime? startDate, DateTime? endDate, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<IncentiveAdhoc>();
            if (!string.IsNullOrWhiteSpace(incentiveAdhocName))
            {
                querry = querry.Where(x => x.SchemeName.ToUpper() == incentiveAdhocName.ToUpper());
            }
            if (startDate.HasValue && endDate.HasValue)
            {
                var fromdate = startDate.Value.Date;
                var todate = endDate.Value.Date.AddDays(1);
                querry = querry.Where(x => x.StartDate >= fromdate && x.EndDate < todate);
            }

            querry = querry.OrderBy(x => x.SchemeId);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public IncentiveAdhoc RetrieveIncentiveAdhocBySchemeId(string SchemeId)
        {
            var record = SchemeId ?? string.Empty;
            return repository.FindOne<IncentiveAdhoc>(x => x.SchemeId == SchemeId);
        }

        public bool Add(IncentiveAdhoc record)
        {
            try
            {
                repository.Add<IncentiveAdhoc>(record);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }

        public bool Update(IncentiveAdhoc record)
        {
            try
            {
                var updateRecord = RetrieveIncentiveAdhocBySchemeId(record.SchemeId);
                if (updateRecord == null) return false;
                updateRecord.SchemeName = record.SchemeName;
                updateRecord.BilltoCode = record.BilltoCode;
                updateRecord.BilltoYn = record.BilltoYn;
                updateRecord.CloseDate = record.CloseDate;
                updateRecord.CloseFlag = record.CloseFlag;
                updateRecord.CloseUser = record.CloseUser;
                updateRecord.UpdateDate = DateTime.Now;
                updateRecord.CreateUser = record.CreateUser;
                updateRecord.EndDate = record.EndDate;
                updateRecord.Mdi = record.Mdi;
                updateRecord.Purpose = record.Purpose;
                updateRecord.PurposeType = record.PurposeType;
                updateRecord.Region = record.Region;
                updateRecord.StartDate = updateRecord.StartDate;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch(Exception exp)
            {
                return false;
            }
        }

        public bool Delete(string schemeId)
        {
            try
            {
                repository.Delete<IncentiveAdhoc>(x => x.SchemeId.ToUpper() == schemeId.ToUpper());
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch (Exception exp)
            {
                return false;
            }
        }
    }
}
